#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/cpufreq.h>
#include <linux/io.h>

#include <asm/div64.h>

#include <asm/mach/map.h>
#include <mach/regs-lcd.h>
#include <mach/regs-gpio.h>
#include <mach/fb.h>

static struct fb_info *fbinfo;


static struct fb_ops lcd_ops = {
	.owner 			= THIS_MODULE,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
};


int __init lcd_drv_init(void)
{
	dma_addr_t fb_dma;

	/* 	1.分配 fb_info 结构体 */
	fbinfo = framebuffer_alloc(0, NULL);
	if (!fbinfo)
		return -ENOMEM;

	/*	2.设置 fb_info 结构体 */
	// 2.1 设置var：LCD分辨率、颜色格式
	fbinfo->var.xres = 800;
	fbinfo->var.yres = 600;
	fbinfo->var.bits_per_pixel = 16;  /*rgb565*/
	
	fbinfo->var.red.offset = 11;  /* 数据偏移，从右往左偏 */
	fbinfo->var.red.length = 5;

	fbinfo->var.green.offset = 5;
	fbinfo->var.green.length = 6;

	fbinfo->var.blue.offset = 0;
	fbinfo->var.blue.length = 5;
	
	
	// 2.2 设置fix：显存
	fbinfo->fix.smem_start = ;  /* fb的物理起始地址 */
	fbinfo->fix.smem_len = fbinfo->var.xres * fbinfo->var.yres * fbinfo->var.bits_per_pixel / 8;
	if(fbinfo->var.bits_per_pixel == 24)
		fbinfo->fix.smem_len = fbinfo->var.xres * fbinfo->var.yres * 4;

	fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;  /* 包装像素 */
	fbinfo->fix.visual = FB_VISUAL_TRUECOLOR;  /* 真彩色 */

	/* fb的虚拟起始地址 */
	fbinfo->screen_base = dma_alloc_writecombine(NULL, fbinfo->fix.smem_len,
						   &fb_dma, GFP_KERNEL);

	// 2.3 设置fbops
	fbinfo->fbops = &lcd_ops;

	/* 	3.注册 fb_info 结构体 */
	register_framebuffer(fbinfo);

	/* 4.硬件操作 */

	return 0;
}

void lcd_drv_exit(void)
{
	/* 反注册fb_info */
	unregister_framebuffer(fbinfo);
	/* 释放fb_info */
	framebuffer_release(fbinfo);
}

module_init(lcd_drv_init);
module_exit(lcd_drv_exit);

MODULE_AUTHOR("HC <chenshao@163.com>");
MODULE_DESCRIPTION("Framebuffer driver for the imx6ull");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:imx6ull-lcd");


